package com.fruit.controller.wap.pic;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.joe.utils.common.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.fruit.controller.base.BaseController;
import com.fruit.service.pic.PictureManager;
import com.fruit.util.MessagesCode;
import com.fruit.util.ResultInfo;

/**
 * 身份证上传
 *
 * @author Administrator
 * @version 2017.12.01 11:40
 */
@Controller
@RequestMapping(value = "/wap/")
public class PicController extends BaseController {

    @Autowired
    private PictureManager pictureService;

    /**
     * 身份证上传
     *
     * @param session session
     * @param request request
     * @return Map<String, Object>
     */
    @RequestMapping(value = "user/cardPic", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Map<String, Object> appVersionInfo(HttpSession session, HttpServletRequest request) {
        this.logBefore(logger, "身份证上传");
        Map<String, Object> resMap = new HashMap<String, Object>();
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == 0 || userId == null) {
            resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.LOGINOUT, "用户未登录!"));
            return resMap;
        }
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile imgfront = multipartRequest.getFile("imgFront");
        MultipartFile imgCon = multipartRequest.getFile("imgCon");
        if (imgfront.isEmpty() && imgCon.isEmpty()) {
            resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.PARAMS_ERROR, "参数错误!"));
            return resMap;
        }
        try {
            InputStream frontIn = imgfront.getInputStream();
            InputStream conin = imgfront.getInputStream();
            String path = "fruit/user/" + userId + "/imgFront.png";
//			Map imgFront = pictureService.uploadPicture(path, frontIn);
            Map imgFront = pictureService.uploadPicture(path, IOUtils.read(frontIn, 1024));
            if (!imgFront.isEmpty()) {
                String pat = "fruit/user/" + userId + "/imgCon.png";
//                Map imgcon = pictureService.uploadPicture(pat, conin);
                Map imgcon = pictureService.uploadPicture(pat, IOUtils.read(conin, 1024));
                int state = (int) imgFront.get("error");
                int stat = (int) imgcon.get("error");
                if (state == 0) {
                    if (stat == 0) {
                        String fronturl = (String) imgFront.get("url");
                        String conurl = (String) imgcon.get("url");
                        resMap.put("result", this.initResult(ResultInfo.SUCCESS, MessagesCode.SUCCESS, "上传成功!"));
                        resMap.put("imgFront", fronturl);
                        resMap.put("imgCon", conurl);
                    } else {
                        resMap.put("result", this.initResult(ResultInfo.ERROR, "error", "上传失败!"));
                        return resMap;
                    }
                } else {
                    resMap.put("result", this.initResult(ResultInfo.ERROR, "error", "上传失败!"));
                    return resMap;
                }
            } else {
                resMap.put("result", this.initResult(ResultInfo.ERROR, "error", "上传失败!"));
                return resMap;
            }
        } catch (Exception e) {
            e.printStackTrace();
            this.logError(logger, e.getMessage());
            resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
        }

        this.logAfter(logger);
        return resMap;

    }
}
